package com.bluemobi.system.service.impl;

import java.util.Map;

import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
import org.springframework.stereotype.Service;

import com.bluemobi.framework.base.Page;
import com.bluemobi.framework.base.impl.BaseServiceImpl;
import com.bluemobi.framework.common.SysUtil;
import com.bluemobi.framework.common.TimeHelper;
import com.bluemobi.framework.exception.SystemException;
import com.bluemobi.framework.log.anotation.LogAspect;
import com.bluemobi.system.model.SysUser;
import com.bluemobi.system.service.SysUserService;

/**
 * 系统用户管理service接口实现类
 *
 * @author zhangyong
 * @version 1.0
 * @Date 2016-3-9 11:28
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl implements SysUserService {

	private static final String DEFAULT_PASSWORD = "123123";

	/**
	 * 根据登录名称获取系统用户登录信息
	 *
	 * @param loginName
	 *            登录名称
	 * @return SysUser
	 * @throws Exception
	 */
	@Override
	public SysUser getSysUserByLoginName(String loginName) throws Exception {
		return dbManage.selectByCondition(loginName);
	}

	/**
	 * 添加用户
	 *
	 * @param sysUser
	 *            用户信息
	 * @return boolean
	 * @throws Exception
	 */
	@LogAspect(desc = "添加系统用户")
	@Override
	public boolean saveSysUser(SysUser sysUser) throws Exception {
		if (null != getSysUserByLoginName(sysUser.getLoginName())) {
			throw SystemException.businessException("登录名称重复");
		}
		sysUser.setSuId(SysUtil.GetUUID());
		sysUser.setCreateDate(TimeHelper.getLocatlTime());
		sysUser.setPassword(new ShaPasswordEncoder().encodePassword(sysUser.getLoginName(), DEFAULT_PASSWORD));
		if (saveSelective(sysUser)) {
			return true;
		}
		throw SystemException.businessException("添加用戶失败");
	}

	/**
	 * 修改用户
	 *
	 * @param sysUser
	 *            用户信息
	 * @return boolean
	 * @throws Exception
	 */
	@LogAspect(desc = "修改系统用户")
	@Override
	public boolean updateSysUser(SysUser sysUser) throws Exception {
		SysUser user = getSysUserByLoginName(sysUser.getLoginName());
		if (null != user) {
			if (!user.getSuId().equals(sysUser.getSuId())) {
				throw SystemException.businessException("登录名称重复");
			}
		}
		if (updateSelective(sysUser)) {
			return true;
		}
		throw SystemException.businessException("修改用戶失败");
	}

	/**
	 * 删除用户
	 *
	 * @param suId
	 *            用户ID
	 * @return boolean
	 * @throws Exception
	 */
	@LogAspect(desc = "删除系统用户")
	@Override
	public boolean delUserBySuId(String suId) throws Exception {
		if (delete(suId)) {
			return true;
		}
		throw SystemException.businessException("未找到要删除的用户");
	}

	/**
	 * 重置用户密码
	 *
	 * @param suId
	 *            用户ID
	 * @param loginName
	 *            登录名称
	 * @return boolean
	 * @throws Exception
	 */
	@LogAspect(desc = "重置用户密码")
	@Override
	public boolean updatePwd(String suId, String loginName) throws Exception {
		SysUser sysUser = new SysUser();
		sysUser.setSuId(suId);
		sysUser.setPassword(new ShaPasswordEncoder().encodePassword(loginName, DEFAULT_PASSWORD));
		if (updateSelective(sysUser)) {
			return true;
		}
		throw SystemException.businessException("未找到用户");
	}

	/**
	 * 用户修改密码
	 *
	 * @param sysUser
	 *            用户信息
	 * @param newPwd
	 *            新密码
	 * @return boolean
	 * @throws Exception
	 */
	@Override
	public boolean updatePwd(SysUser sysUser, String newPwd) throws Exception {
		SysUser user = getSysUserByLoginName(sysUser.getLoginName());
		String pwd = new ShaPasswordEncoder().encodePassword(sysUser.getLoginName(), sysUser.getPassword());
		if (!pwd.equals(user.getPassword())) {
			throw SystemException.businessException("旧密码不正确");
		}
		sysUser.setPassword(new ShaPasswordEncoder().encodePassword(sysUser.getLoginName(), newPwd));
		if (updateSelective(sysUser)) {
			return true;
		}
		throw SystemException.businessException("未找到用户");
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Page getUserListByOrgIds(Page page, Map searchParams) throws Exception {
		searchParams.put("sort", page.getSort());
		return dbManage.selectPageByCondition(searchParams, page);
	}
}
